package com.example.coffee.slice;

import com.example.coffee.ResourceTable;
import com.example.coffee.items.CaffeeItem;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;

public class DetailSlice extends AbilitySlice {

    private CaffeeItem caffeeItem = null;

    private String sizeOption = "";
    private String temperatureOption = "";
    private String sweetOption = "";

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        caffeeItem = new CaffeeItem(intent.getIntParam("image", 0), intent.getStringParam("name"), intent.getStringParam("price"), intent.getStringParam("description"), intent.getStringParam("originalPrice"));
        setUIContent(ResourceTable.Layout_detail_slice);
        setup();
    }

    private void setup() {
        Image image = (Image) findComponentById(ResourceTable.Id_detail_image);
        image.setPixelMap(caffeeItem.getImageId());

        Text nameText = (Text) findComponentById(ResourceTable.Id_detail_title);
        nameText.setText(caffeeItem.getName());

        Text detailText = (Text) findComponentById(ResourceTable.Id_detail_desc);
        detailText.setText(caffeeItem.getDescription());

        Text priceText = (Text) findComponentById(ResourceTable.Id_detail_price);
        priceText.setText("优惠价格：" + caffeeItem.getPrice());

        Text op1_1 = (Text) findComponentById(ResourceTable.Id_detail_op1_1);
        Text op1_2 = (Text) findComponentById(ResourceTable.Id_detail_op1_2);
        Text op2_1 = (Text) findComponentById(ResourceTable.Id_detail_op2_1);
        Text op2_2 = (Text) findComponentById(ResourceTable.Id_detail_op2_2);
        Text op3_1 = (Text) findComponentById(ResourceTable.Id_detail_op3_1);
        Text op3_2 = (Text) findComponentById(ResourceTable.Id_detail_op3_2);

        op1_1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                op1_1.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_selected));
                op1_2.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_normal));
                sizeOption = "大杯";
            }
        });
        op1_2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                op1_2.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_selected));
                op1_1.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_normal));
                sizeOption = "小杯";
            }
        });
        op2_1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                op2_1.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_selected));
                op2_2.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_normal));
                temperatureOption = "冷";
            }
        });
        op2_2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                op2_2.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_selected));
                op2_1.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_normal));
                temperatureOption = "热";
            }
        });
        op3_1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                op3_1.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_selected));
                op3_2.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_normal));
                sweetOption = "全糖";
            }
        });
        op3_2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                op3_2.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_selected));
                op3_1.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_background_option_normal));
                sweetOption = "半糖";
            }
        });

        Button button = (Button) findComponentById(ResourceTable.Id_detail_buy);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // 购买逻辑
                Intent intent = new Intent();
                intent.setParam("id", "123456789");
                present(new OrderSlice(), intent);
            }
        });
    }
}
